package com.jake.spring.boot.csv.controller;

import com.google.common.collect.Lists;
import com.jake.spring.boot.csv.utils.CSVUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("csv")
public class CSVController {

    @GetMapping("download")
    public void download(HttpServletResponse response) {
        File file = CSVUtils.createTemp(setUpHeaders(), setUpLargeValuesList());
        CSVUtils.download(file, "小区记录", response);
    }

    private List<String> setUpHeaders() {
        return Lists.newArrayList("序号", "小区", "地址");
    }

    private List<List<String>> setUpValuesList() {
        return Lists.newArrayList(
                Lists.newArrayList("1", "德意名居", "大学城"),
                Lists.newArrayList("2", "半岛城邦", "深圳湾"),
                Lists.newArrayList("3", "恒裕滨城", "深圳湾"),
                Lists.newArrayList("4", "中洲华府", "新安")
        );
    }

    private List<List<String>> setUpLargeValuesList() {
        List<List<String>> valuesList = Lists.newArrayListWithExpectedSize(100000);
        for (int i = 0; i < 100000; i++) {
            valuesList.add(Lists.newArrayList(Integer.toString(i), "小区" + i, "片区" + i));
        }
        return valuesList;
    }

}
